package com.changgou.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.DeleteListenPoint;
import com.xpand.starter.canal.annotation.InsertListenPoint;
import com.xpand.starter.canal.annotation.UpdateListenPoint;

import java.util.List;

/**
 * @ClassName CanalEnvenListenter
 * @Description 监控数据数据
 * @Author 洛尘
 * @CanalEventListener 事件监听器
 * @Date 20:44 2019/11/14
 * @Version 2.1
 **/
@CanalEventListener
public class CanalDataEnventListener {

    /**
     *  添加的时候
      * @param entryType 监控对数据库操作的事件
     * @param rowData 操作行数据
     */
    @InsertListenPoint
    public void onEnventInsert(CanalEntry.EntryType entryType,CanalEntry.RowData rowData){
        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
        for (CanalEntry.Column column : afterColumnsList) {
            String columnName = column.getName(); // 列的名称
            String value = column.getValue();   //列对应的值
            System.out.println("添加列名称:" + columnName +"=列值===="+ value);
        }
    }


    @UpdateListenPoint
    public void onEnvenUpdate(CanalEntry.EntryType entryType ,CanalEntry.RowData rowData){
        List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();
        for (CanalEntry.Column column : beforeColumnsList) {
            String columnName = column.getName(); // 列的名称
            String value = column.getValue();   //列对应的值
            System.out.println("列名称:" + columnName +"====="+ value);
        }
        System.out.println("=====更改后=======");
        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
        for (CanalEntry.Column column : afterColumnsList) {
            String columnName = column.getName(); // 列的名称
            String value = column.getValue();   //列对应的值
            System.out.println("列名称:" + columnName +"====="+ value);
        }
    }

    @DeleteListenPoint
    public void onEnvenDelete(CanalEntry.EntryType entryType,CanalEntry.RowData rowData){
        List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();
        for (CanalEntry.Column column : beforeColumnsList) {
            String value = column.getValue();
            String columnName = column.getName();
            System.out.println("列名称:" + columnName +"====="+ value);
        }
    }

}
